
import org.springframework.web.servlet.ModelAndView

import org.authsum.stitches.*
import org.authsum.stitches.external.*

class ContentDefController {

	def stitchesService
	
	def helper
	
	def sampledata = {
			
			helper.sampleData()
					chain(action:index)
	}
	def index = { 


		def contentDefRequest = new ContentDefRequest()
		//folderRequest.folderId = params['id'].toLong()
		def contentDefResponse =stitchesService.findContentDefs(contentDefRequest)
		//	  [ childFolders:response] 	
		//println "there are contentDefs=" + response.contentDefs.value.folderHolder.size()
		// redirect(action:index)	
		//chain(action:index,model:[contentDefs:response])
		[contentDefResponse:contentDefResponse]
		
		
		
	}
	
	
    def index2 = { 
    		
    		//get the content from a contentid, contentid is in searchresults
    		
    		//for a given contentdefid (in content), we can get the attributes

			 def contentDefRequest = new ContentDefRequest()
		     contentDefRequest.contentVersionId = params['id'].toLong()
		     def response =stitchesService.findContentDefById(contentDefRequest)
			 		    
			 assert response.responseCode == '200'
				 [ contentDefResponse:response,contentVersionId:params['id'].toLong()] 	
			 		    
			 		    
    		
    }
	
	
	def addAttribute = {
			
		def contentDefRequest = new ContentDefRequest()
		def attributeHolder = new AttributeHolder()
		contentDefRequest.attributeHolder = attributeHolder
		
		println "addAttribute "  + params
		
		bindData(attributeHolder, params)
		bindData(contentDefRequest, params)
		
		assert contentDefRequest.contentDefId > 0
		
		def contentDefResponse =stitchesService.addAttribute(contentDefRequest)	  
				assert contentDefResponse.responseCode == '200'
		contentDefResponse.getMessages()?.string.each {
			println it
			flash.message += it
		}


		chain(action:index)
		
		
	}
	
	
	def removeAttribute = {
			println "removeAttribute "  + params
			

		def contentDefRequest = new ContentDefRequest()
		def attributeHolder = new AttributeHolder()
		contentDefRequest.attributeHolder = attributeHolder
		//request.attributeHolder.id


		
	//not setting id in bindData
			bindData(attributeHolder, params)
			println "After Bind:  attributeholder.id=" + attributeHolder.id
			attributeHolder.id = params['attributeId'].toLong()
					println "After Manually Setting:  attributeholder.id=" + attributeHolder.id
					
		bindData(contentDefRequest, params)
		def contentDefResponse =stitchesService.removeAttribute(contentDefRequest)	  

		contentDefResponse.getMessages()?.string.each {
			println it
			flash.message += it
		}
		
			assert contentDefResponse.responseCode == '200'
		chain(action:index)
		
		
	}
	
	def editContentDef = {
		//get a contentdef an pop it into a form
	
		def contentDefRequest = new ContentDefRequest()
		contentDefRequest.contentDefId  = params['id'].toLong()
		def contentDefResponse =stitchesService.findContentDefById(contentDefRequest)
		def contentDefHolder = contentDefResponse.contentDefHolders.contentDefHolder.get(0)
				def attributeHolder = new AttributeHolder()
		return new ModelAndView("/contentDef/contentDefForm",  [contentDefResponse:contentDefResponse,contentDefHolder:contentDefHolder,contentDefRequest:contentDefRequest,attributeHolder:attributeHolder])
		
		
		
	}
	
	def contentDefForm = {
	}
	
	def createContentDefForm = {
			
					def contentDefHolder = new ContentDefHolder()
			return new ModelAndView("/contentDef/contentDefForm",  [contentDefHolder:contentDefHolder])
			
	}
	
	
	def saveContentDef = {
		
			
		def contentDefRequest = new ContentDefRequest()
		def contentDefHolder = new ContentDefHolder()
		contentDefRequest.contentDefHolder = contentDefHolder
		bindData(contentDefHolder, params)
		assert contentDefHolder.name != null
		def contentDefResponse =stitchesService.saveContentDef(contentDefRequest)
		
		assert contentDefResponse.responseCode == '200'
		flash.message = contentDefResponse.messages.string
	   return new ModelAndView("/contentDef/contentDefForm", [contentDefResponse:contentDefResponse])
		
	}
	
}
